﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace SignalBox.Core.Data
{
    public class Track
    {

        public int X
        {
            get;
            set;
        }
        public int Y
        {
            get;
            set;
        }
        public int Type
        {
            get;
            set;
        }
        public int Length
        {
            get;
            set;
        }
        public int MaxSpeedLeft
        {
            get;
            set;
        }
        public int MaxSpeedRight
        {
            get;
            set;
        }
        public int LinkX
        {
            get;
            set;
        }
        public int LinkY
        {
            get;
            set;
        }
        public int SignalAspect
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string Platform
        {
            get;
            set;
        }

        public static List<Track> FromXml(XElement tracks)
        {
            List<Track> result = (from XElement e in tracks.Elements("Track")
                                 select new Track() {
                                     X = Int32.Parse(e.Element("X").Value),
                                     Y = Int32.Parse(e.Element("Y").Value),
                                     LinkX = Int32.Parse(e.Element("LinkX").Value),
                                     LinkY = Int32.Parse(e.Element("LinkY").Value),
                                     Name = e.Element("Name").Value,
                                     Platform = e.Element("Platform").Value,
                                     Length = Int32.Parse(e.Element("Length").Value),
                                     MaxSpeedLeft = Int32.Parse(e.Element("MaxSpeedLeft").Value),
                                     MaxSpeedRight = Int32.Parse(e.Element("MaxSpeedRight").Value),
                                     SignalAspect = Int32.Parse(e.Element("SignalAspect").Value),
                                     Type = Int32.Parse(e.Element("Type").Value)
                                 }).ToList<Track>();

            return result;
        }

        public XElement ToXml()
        {
            return new XElement("Track",
                new XElement("X", X),
                new XElement("Y", Y),
                new XElement("LinkX", LinkX),
                new XElement("LinkY", LinkY),
                new XElement("Name", Name),
                new XElement("Platform", Platform),
                new XElement("Length", Length),
                new XElement("MaxSpeedLeft", MaxSpeedLeft),
                new XElement("MaxSpeedRight", MaxSpeedRight),
                new XElement("SignalAspect", SignalAspect),
                new XElement("Type", Type));

        }
    }
}
